package com.wuxianedu.toolbar;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //设置toolbar
       Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setLogo(R.mipmap.ic_launcher); // 设置logo
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("wuxianedu");
        toolbar.setSubtitle("子标题");
        toolbar.setSubtitleTextColor(Color.GRAY);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.pic);
//        toolbar.setNavigationOnClickListener(); 回退按钮的点击监听
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action1:
                        Toast.makeText(MainActivity.this,"行为1", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action2:
                        Toast.makeText(MainActivity.this,"行为2", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action3:
                        Toast.makeText(MainActivity.this,"行为3", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
    }

    /**
     * 创建toolbar的menu
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tool_menu, menu);
        return true;
    }

    @Override
    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                    Log.e("ssss","ssss");
                }
            }
        }
        return super.onPrepareOptionsPanel(view, menu);
    };
}
